package cn.edu.zjicm.wordsnet_d.view;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.data.CustomDate;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.interfaces.CalendarActivityCallback;
import cn.edu.zjicm.wordsnet_d.util.DateTimeUtils;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;

public class CalendarView extends View implements CalendarActivityCallback {

	private final int TOTAL_COL = 7;
	private final int TOTAL_ROW = 6;

	private final String orangeColor = "#ff7e37";
	private final String greenColor = "#00b081";
	private final String whiteColor = "#fffffe";

	private Paint mCirclePaint;
	private Paint mTextPaint;
	private int mViewWidth;
	private int mViewHeight;
	private int mCellSpace;
	private Row[] rows = new Row[TOTAL_ROW];
	private static CustomDate mShowDate;
	private int touchSlop;
	private boolean callBackCellSpace;
	private Cell yesterdayClickCell, todayClickCell;
	private float mDownX;
	private float mDownY;

	private Context mContext;
	private List<Integer> punchOutDays;
	private CalendarCallback calendarCallback;

	private int monthIndex;
	private int todayYear, todayMonth, today;
	private int yesterdayRow = -1, yesterdayCol = -1;
	private int todayRow = -1, todayCol = -1;
	private boolean yesterdayCanClick = false, todayCanClick = false;
	private int[] color = new int[2];
	private int yesterdayGradientColor, todayGradientColor;
	private float startLeft, startRight, startTop, startBottom;
	private float yesterdayLeft, yesterdayRight, todayLeft, todayRight;

	public interface CalendarCallback {
		void clickToPunch(State state, CalendarActivityCallback calendarActivityCallback);

		void setCurrentItem(int realDate);
	}

	public CalendarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public CalendarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public CalendarView(Context context) {
		super(context);
		init(context);
	}

	public CalendarView(Context context, int monthIndex, CalendarCallback calendarCallback) {
		super(context);
		this.monthIndex = monthIndex;
		this.calendarCallback = calendarCallback;
		init(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		for (int i = 0; i < TOTAL_ROW; i++) {
			if (rows[i] != null) {
				rows[i].drawCells(canvas);
			}
		}
	}

	private void init(Context context) {
		mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mCirclePaint.setStyle(Paint.Style.FILL);
		mCirclePaint.setColor(Color.parseColor(orangeColor));
		touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
		mContext = context;
		if (NightModeUtil.isNightModeNow()) {
			color[0] = getResources().getColor(R.color.word_color_night);
			color[1] = getResources().getColor(R.color.calendar_cell_past_night);
		} else {
			color[0] = getResources().getColor(R.color.study_view_text_color);
			color[1] = getResources().getColor(R.color.calendar_cell_past_day);
		}

		todayYear = DateTimeUtils.getYear();
		todayMonth = DateTimeUtils.getMonth();
		today = DateTimeUtils.getCurrentMonthDay();

		initDate();
	}

	private void initDate() {
		mShowDate = new CustomDate(monthIndex);
		fillDate();
	}

	private void fillDate() {
		fillMonthDate();
	}

	/**
	 * 填充数据，通过getWeekDayFromDate()得到一个月第一天是星期几，就可以算出所有日期的位置，然后填充
	 */
	private void fillMonthDate() {
		punchOutDays = WordFactory.getInstance(mContext).getDaysByYearAndMonth(mShowDate.year, mShowDate.month);
		LogUtil.d(punchOutDays.toString());
		// punchOutDays = new ArrayList<Integer>();
		// if(monthIndex==286){
		// punchOutDays.add(11);
		// punchOutDays.add(18);
		// punchOutDays.add(23);
		// }

		int lastMonthDay = DateTimeUtils.getMonthDays(mShowDate.year, mShowDate.month - 1); // 当前月的最后一天
		int currentMonthDays = DateTimeUtils.getMonthDays(mShowDate.year, mShowDate.month); // 当前月的天数
		int firstDayWeek = DateTimeUtils.getWeekDayFromDate(mShowDate.year, mShowDate.month); // 判断指定月的1号是星期几
		int day = 0;
		for (int j = 0; j < TOTAL_ROW; j++) {
			rows[j] = new Row(j);
			for (int i = 0; i < TOTAL_COL; i++) {
				int postion = i + j * TOTAL_COL;
				if (postion >= firstDayWeek && postion < firstDayWeek + currentMonthDays) { // 在当前月的范围内
					day++;
					CustomDate date = CustomDate.modifiDayForObject(mShowDate, day);
					if (punchOutDays.contains(day)) {
						date.week = i;
						if (isToday(date)) {
							rows[j].cells[i] = new Cell(date, State.TODAY_AND_PUNCH, i, j);
						} else {
							rows[j].cells[i] = new Cell(date, State.PUNCH_DAY, i, j);
						}
						continue;
					} else {
						rows[j].cells[i] = new Cell(date, getState(date), i, j);
					}
				} else if (postion < firstDayWeek) {
					rows[j].cells[i] = new Cell(new CustomDate(mShowDate.year, mShowDate.month - 1, lastMonthDay - (firstDayWeek - postion - 1)), State.PAST_MONTH_DAY, i, j);
				} else if (postion >= firstDayWeek + currentMonthDays) {
					rows[j].cells[i] = new Cell((new CustomDate(mShowDate.year, mShowDate.month + 1, postion - firstDayWeek - currentMonthDays + 1)), State.NEXT_MONTH_DAY, i, j);
				}
			}
		}
	}

	private State getState(CustomDate date) {
		String noPunchDate = AppPreference.getNoPunchDate(mContext);
		int year, month, day;
		State state = State.CURRENT_MONTH_DAY;

		if (noPunchDate.length() > 0) {
			String[] arr = noPunchDate.split(";");

			if (arr.length == 1) {
				year = Integer.valueOf(arr[0].substring(0, 4)); // 未打卡的年月日
				month = Integer.valueOf(arr[0].substring(4, 6));
				day = Integer.valueOf(arr[0].substring(6, 8));

				if (date.year == year && date.month == month && date.day == day) {
					if (isToday(date)) {
						todayCanClick = true;
						state = State.TODAY_AND_NO_PUNCH;
						// LogUtil.d("Tag", "day=" + date.day +
						// ",今天完成任务，还没有打卡");
					} else {
						yesterdayCanClick = true;
						state = State.YESTERDAY_AND_NO_PUNCH;
						// LogUtil.d("Tag", "day=" + date.day +
						// ",昨天完成任务，还没有打卡");
					}
				} else {
					if (isToday(date)) {
						state = State.TODAY;
						// LogUtil.d("Tag", "day=" + date.day + ",当天");
					} else {
						state = State.CURRENT_MONTH_DAY;
						// LogUtil.d("Tag", "day=" + date.day + ",当前月");
					}
				}

			} else {
				int arr0 = Integer.valueOf(arr[0]);
				int arr1 = Integer.valueOf(arr[1]);

				int max = arr0 > arr1 ? arr0 : arr1;

				for (int i = 0; i < arr.length; i++) {
					year = Integer.valueOf(arr[i].substring(0, 4));
					month = Integer.valueOf(arr[i].substring(4, 6));
					day = Integer.valueOf(arr[i].substring(6, 8));

					if (date.year == year && date.month == month && date.day == day) {
						if (i == 0) {
							if (max == arr0) { // 说明是保存的两个日期中大的
								todayCanClick = true;
								if (isToday(date)) {
									state = State.TODAY_AND_NO_PUNCH;
									// LogUtil.d("Tag", "day=" + date.day +
									// ",今天完成任务，还没有打卡");
								} else {
									state = State.FAKE_YESTERDAY_AND_NO_PUNCH;
									// LogUtil.d("Tag", "day=" + date.day +
									// ",真实的今天，但没有下划线，还没有打卡");
								}
							} else { // 说明是保存的两个日期中小的
								yesterdayCanClick = true;
								if (isToday(date)) {
									state = State.FAKE_TODAY_AND_NO_PUNCH;
									// LogUtil.d("Tag", "day=" + date.day +
									// ",真实的昨天，但是有下划线，还没有打卡");
								} else {
									state = State.YESTERDAY_AND_NO_PUNCH;
									// LogUtil.d("Tag", "day=" + date.day +
									// ",昨天完成任务，还没有打卡");
								}
							}
						} else if (i == 1) {
							if (max == arr0) { // 说明是保存的两个日期中小的
								yesterdayCanClick = true;
								if (isToday(date)) {
									state = State.FAKE_TODAY_AND_NO_PUNCH;
									// LogUtil.d("Tag", "day=" + date.day +
									// ",真实的昨天，但是有下划线，还没有打卡");
								} else {
									state = State.YESTERDAY_AND_NO_PUNCH;
									// LogUtil.d("Tag", "day=" + date.day +
									// ",昨天完成任务，还没有打卡");
								}
							} else { // 说明是保存的两个日期中大的
								todayCanClick = true;
								if (isToday(date)) {
									state = State.TODAY_AND_NO_PUNCH;
									// LogUtil.d("Tag", "day=" + date.day +
									// ",今天完成任务，还没有打卡");
								} else {
									state = State.FAKE_YESTERDAY_AND_NO_PUNCH;
									// LogUtil.d("Tag", "day=" + date.day +
									// ",真实的今天，但是没有下划线，还没有打卡");
								}
							}
						}
					}
				}
			}
		} else {
			if (isToday(date)) {
				state = State.TODAY;
				// LogUtil.d("Tag", "day=" + date.day + ",当天");
			} else {
				state = State.CURRENT_MONTH_DAY;
				// LogUtil.d("Tag", "day=" + date.day + ",当前月");
			}
		}
		return state;
	}

	private boolean isToday(CustomDate showDate) {
		if (showDate.year == todayYear && showDate.month == todayMonth && showDate.day == today) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mViewWidth = w;
		mViewHeight = h;
		mCellSpace = Math.min(mViewHeight / TOTAL_ROW, mViewWidth / TOTAL_COL);
		if (!callBackCellSpace) {
			callBackCellSpace = true;
		}
		mTextPaint.setTextSize(mCellSpace / 3);
	}

	/*
	 * 
	 * 触摸事件为了确定点击的位置日期
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mDownX = event.getX();
			mDownY = event.getY();
			break;
		case MotionEvent.ACTION_UP:
			float disX = event.getX() - mDownX;
			float disY = event.getY() - mDownY;
			if (Math.abs(disX) < touchSlop && Math.abs(disY) < touchSlop) {
				int col = (int) (mDownX / mCellSpace);
				int row = (int) (mDownY / mCellSpace);
				measureClickCell(col, row);
			}
			break;
		}
		return true;
	}

	private Cell yesterdayCellTemp, todayCellTemp;

	private void measureClickCell(int col, int row) {
		LogUtil.d("Tag", "调用measureClickCell,row=" + row + ",col=" + col + ",yesterdayRow=" + yesterdayRow + ",yesterdayCol=" + yesterdayCol + ",todayRow=" + todayRow + ",todayCol=" + todayCol);
		if (col >= TOTAL_COL || row >= TOTAL_ROW)
			return;

		if (row == yesterdayRow && col == yesterdayCol && yesterdayCanClick) {
			LogUtil.d("Tag", "点击昨天");

			yesterdayClickCell = new Cell(rows[row].cells[col].date, State.CLICK_YESTERDAY, rows[row].cells[col].i, rows[row].cells[col].j);
			yesterdayCellTemp = rows[yesterdayClickCell.j].cells[yesterdayClickCell.i]; // 临时保存
			rows[yesterdayClickCell.j].cells[yesterdayClickCell.i] = yesterdayClickCell;

			calendarCallback.clickToPunch(State.YESTERDAY, this);
		}

		if (row == todayRow && col == todayCol && todayCanClick) {
			LogUtil.d("Tag", "点击今天");

			todayClickCell = new Cell(rows[row].cells[col].date, State.CLICK_TODAY, rows[row].cells[col].i, rows[row].cells[col].j);
			todayCellTemp = rows[todayClickCell.j].cells[todayClickCell.i];
			rows[todayClickCell.j].cells[todayClickCell.i] = todayClickCell;
			calendarCallback.clickToPunch(State.TODAY, this);
		}
	}

	public void resetCell(State state) {
		if (state == State.YESTERDAY) {
			rows[yesterdayClickCell.j].cells[yesterdayClickCell.i] = yesterdayCellTemp;
		} else {
			rows[todayClickCell.j].cells[todayClickCell.i] = todayCellTemp;
		}
	}

	/**
	 * 这里的date是服务器真实打卡日期,需要跟本地匹配看是不是当前显示的那个日期
	 */
	@Override
	public void clickAnimation(State state, int realDate) {
		LogUtil.d("调用clickAnimation,realDate=" + realDate);

		String s_realDate = "20" + realDate + "";

		int realYear = Integer.valueOf(s_realDate.substring(0, 4)); // 未打卡的年月日
		int realMonth = Integer.valueOf(s_realDate.substring(4, 6));
		int realDay = Integer.valueOf(s_realDate.substring(6, 8));

		if (state == State.TODAY) {
			CustomDate customDate = todayClickCell.date;
			if (realYear == customDate.year && realMonth == customDate.month && realDay == customDate.day) {
				startAnimation(state, todayClickCell);
			} else if (realYear == customDate.year && realMonth == customDate.month) {
				// 原来错的位置
				Cell wrongCell = new Cell(customDate, State.CURRENT_MONTH_DAY, todayClickCell.i, todayClickCell.j);
				rows[wrongCell.j].cells[wrongCell.i] = wrongCell;
				// LogUtil.d("错误的位置,(" + wrongCell.j + "," + wrongCell.i);

				// 正确的位置
				changeCellPosition(todayClickCell, realDay);
				customDate = new CustomDate(realYear, realMonth, realDay);
				todayClickCell.date = customDate;
				rows[todayClickCell.j].cells[todayClickCell.i] = todayClickCell;
				// LogUtil.d("正确的位置,(" + todayClickCell.j + "," +
				// todayClickCell.i);
				startAnimation(state, todayClickCell);
			} else { // 说明不是当前月
				// LogUtil.d("不是当前月");
				Cell wrongCell = new Cell(customDate, State.TODAY, todayClickCell.i, todayClickCell.j);
				rows[wrongCell.j].cells[wrongCell.i] = wrongCell;
				invalidate();
				calendarCallback.setCurrentItem(realDate);
			}
		} else if (state == State.YESTERDAY) {
			CustomDate customDate = yesterdayClickCell.date;
			if (realYear == customDate.year && realMonth == customDate.month && realDay == customDate.day) {
				// LogUtil.d("是当天");
				startAnimation(state, yesterdayClickCell);
			} else if (realYear == customDate.year && realMonth == customDate.month) {
				// LogUtil.d("不是当天");
				// 原来错的位置
				Cell wrongCell = new Cell(customDate, State.CURRENT_MONTH_DAY, yesterdayClickCell.i, yesterdayClickCell.j);
				rows[wrongCell.j].cells[wrongCell.i] = wrongCell;
				// LogUtil.d("错误的位置,(" + wrongCell.j + "," + wrongCell.i + ")" +
				// ",oldDay=" + yesterdayClickCell.date.day);

				// 正确的位置
				changeCellPosition(yesterdayClickCell, realDay);
				customDate = new CustomDate(realYear, realMonth, realDay);
				yesterdayClickCell.date = customDate;
				rows[yesterdayClickCell.j].cells[yesterdayClickCell.i] = yesterdayClickCell;
				// LogUtil.d("正确的位置,(" + yesterdayClickCell.j + "," +
				// yesterdayClickCell.i);
				startAnimation(state, yesterdayClickCell);
			} else { // 说明不是当前月
				// LogUtil.d("不是当前月");
				Cell wrongCell = new Cell(customDate, State.CURRENT_MONTH_DAY, yesterdayClickCell.i, yesterdayClickCell.j);
				rows[wrongCell.j].cells[wrongCell.i] = wrongCell;
				invalidate();
				calendarCallback.setCurrentItem(realDate);
			}
		}

	}

	private void changeCellPosition(Cell cell, int realDay) {
		int oldDay = cell.date.day;
		// LogUtil.d("修改前(" + cell.j + "," + cell.i + ")" + ",realDay:" +
		// realDay + ",oldDay:" + oldDay);
		int row = cell.j, col = cell.i;

		int newRow = 0, newCol = 0;

		int z = realDay - oldDay;

		if (z > 0) {
			if ((6 - col) >= z) {
				newRow = row;
				newCol = col + z;
			} else {
				row++;
				z = z - (7 - col);
				newRow = row + (z / 7);
				newCol = z % 7;
			}
		} else {
			z = -z;
			if (col >= z) {
				newRow = row;
				newCol = col - z;
			} else {
				z = z - col;
				row--;
				if (z >= 7) {
					newRow = row - (z / 7);
					newCol = 7 - (z % 7);
				} else {
					newRow = row;
					newCol = 7 - z;
				}
			}
		}

		cell.j = newRow;
		cell.i = newCol;

		// LogUtil.d("修改后(" + cell.j + "," + cell.i + ")");
	}

	private void startAnimation(final State state, Cell cell) {

		startLeft = (float) (mCellSpace * (cell.i + 0.5)) - mCellSpace / 3;
		startRight = (float) (mCellSpace * (cell.i + 0.5)) + mCellSpace / 3;
		startTop = (float) ((cell.j + 0.5) * mCellSpace) - mCellSpace / 3;
		startBottom = (float) ((cell.j + 0.5) * mCellSpace) + mCellSpace / 3;

		new Thread() {
			public void run() {
				int degree = 20;
				if (state == CalendarView.State.YESTERDAY) {
					yesterdayGradientColor = Color.parseColor(greenColor);
				} else {
					todayGradientColor = Color.parseColor(greenColor);
				}

				for (int i = 0; i <= degree; i++) {
					if (state == CalendarView.State.YESTERDAY) {
						yesterdayLeft = startLeft + (startRight - startLeft) / 2 / degree * i;
						yesterdayRight = startRight - (startRight - startLeft) / 2 / degree * i;
					} else {
						todayLeft = startLeft + (startRight - startLeft) / 2 / degree * i;
						todayRight = startRight - (startRight - startLeft) / 2 / degree * i;
					}
					mHanlder.sendEmptyMessage(0);
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

				if (state == CalendarView.State.YESTERDAY) {
					yesterdayGradientColor = Color.parseColor(orangeColor);
				} else {
					todayGradientColor = Color.parseColor(orangeColor);
				}

				for (int i = 0; i <= degree; i++) {
					if (state == CalendarView.State.YESTERDAY) {
						yesterdayLeft = (startRight + startLeft) / 2 - (startRight - startLeft) / 2 / degree * i;
						yesterdayRight = (startRight + startLeft) / 2 + (startRight - startLeft) / 2 / degree * i;
					} else {
						todayLeft = (startRight + startLeft) / 2 - (startRight - startLeft) / 2 / degree * i;
						todayRight = (startRight + startLeft) / 2 + (startRight - startLeft) / 2 / degree * i;
					}

					mHanlder.sendEmptyMessage(0);
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			};
		}.start();
	}

	Handler mHanlder = new Handler() {
		public void handleMessage(Message msg) {
			invalidate();
		};
	};

	/**
	 * 组
	 */
	class Row {
		public int j;

		Row(int j) {
			this.j = j;
		}

		public Cell[] cells = new Cell[TOTAL_COL];

		public void drawCells(Canvas canvas) {
			for (int i = 0; i < cells.length; i++) {
				if (cells[i] != null) {
					cells[i].drawSelf(canvas);
				}
			}
		}
	}

	/**
	 * 单元格
	 */
	class Cell {
		public CustomDate date;
		public State state;
		public int i; // 列
		public int j; // 行

		public Cell(CustomDate date, State state, int i, int j) {
			super();
			this.date = date;
			this.state = state;
			this.i = i;
			this.j = j;
		}

		// 绘制一个单元格
		public void drawSelf(Canvas canvas) {

			// 绘制文字
			String content = date.day + "";
			float measureValue = mTextPaint.measureText(content);

			switch (state) {
			case CURRENT_MONTH_DAY:
				mTextPaint.setColor(color[0]);
				break;
			case NEXT_MONTH_DAY:
			case PAST_MONTH_DAY:
				mTextPaint.setColor(color[1]);
				break;

			case TODAY:
				mTextPaint.setColor(Color.parseColor(orangeColor));
				mTextPaint.setStrokeWidth(3);
				canvas.drawLine((float) ((i + 0.5) * mCellSpace - measureValue / 2), (float) ((j + 0.7) * mCellSpace), (float) ((i + 0.5) * mCellSpace + measureValue / 2),
						(float) ((j + 0.7) * mCellSpace), mTextPaint);
				mTextPaint.setColor(color[0]);
				break;

			case PUNCH_DAY:
				mCirclePaint.setColor(Color.parseColor(orangeColor));
				mTextPaint.setColor(Color.parseColor(whiteColor));
				canvas.drawCircle((float) (mCellSpace * (i + 0.5)), (float) ((j + 0.5) * mCellSpace), mCellSpace / 3, mCirclePaint);
				break;

			case TODAY_AND_PUNCH:
				mCirclePaint.setColor(Color.parseColor(orangeColor));
				mTextPaint.setColor(Color.parseColor(whiteColor));
				mTextPaint.setStrokeWidth(3);
				canvas.drawCircle((float) (mCellSpace * (i + 0.5)), (float) ((j + 0.5) * mCellSpace), mCellSpace / 3, mCirclePaint);
				canvas.drawLine((float) ((i + 0.5) * mCellSpace - measureValue / 2), (float) ((j + 0.7) * mCellSpace), (float) ((i + 0.5) * mCellSpace + measureValue / 2),
						(float) ((j + 0.7) * mCellSpace), mTextPaint);
				break;

			case TODAY_AND_NO_PUNCH:
				mTextPaint.setColor(Color.parseColor(whiteColor));
				mTextPaint.setStrokeWidth(3);
				mCirclePaint.setColor(Color.parseColor(greenColor));
				canvas.drawCircle((float) (mCellSpace * (i + 0.5)), (float) ((j + 0.5) * mCellSpace), mCellSpace / 3, mCirclePaint);
				canvas.drawLine((float) ((i + 0.5) * mCellSpace - measureValue / 2), (float) ((j + 0.7) * mCellSpace), (float) ((i + 0.5) * mCellSpace + measureValue / 2),
						(float) ((j + 0.7) * mCellSpace), mTextPaint);
				todayRow = j;
				todayCol = i;
				break;

			case YESTERDAY_AND_NO_PUNCH:
				mTextPaint.setColor(Color.parseColor(whiteColor));
				mCirclePaint.setColor(Color.parseColor(greenColor));
				canvas.drawCircle((float) (mCellSpace * (i + 0.5)), (float) ((j + 0.5) * mCellSpace), mCellSpace / 3, mCirclePaint);
				yesterdayRow = j;
				yesterdayCol = i;
				break;

			case FAKE_TODAY_AND_NO_PUNCH: // 是两个日期中小的，而且是手机端的‘今天’
				mTextPaint.setColor(Color.parseColor(whiteColor));
				mTextPaint.setStrokeWidth(3);
				mCirclePaint.setColor(Color.parseColor(greenColor));
				canvas.drawCircle((float) (mCellSpace * (i + 0.5)), (float) ((j + 0.5) * mCellSpace), mCellSpace / 3, mCirclePaint);
				canvas.drawLine((float) ((i + 0.5) * mCellSpace - measureValue / 2), (float) ((j + 0.7) * mCellSpace), (float) ((i + 0.5) * mCellSpace + measureValue / 2),
						(float) ((j + 0.7) * mCellSpace), mTextPaint);
				yesterdayRow = j;
				yesterdayCol = i;
				break;
			case FAKE_YESTERDAY_AND_NO_PUNCH: // 是两个日期中大的，但不是手机端的‘今天’
				mTextPaint.setColor(Color.parseColor(whiteColor));
				mCirclePaint.setColor(Color.parseColor(greenColor));
				canvas.drawCircle((float) (mCellSpace * (i + 0.5)), (float) ((j + 0.5) * mCellSpace), mCellSpace / 3, mCirclePaint);
				todayRow = j;
				todayCol = i;
				break;

			case CLICK_YESTERDAY:
				yesterdayCanClick = false;
				mTextPaint.setColor(Color.parseColor(whiteColor));
				mCirclePaint.setColor(yesterdayGradientColor);

				RectF oval = new RectF(yesterdayLeft, startTop, yesterdayRight, startBottom);
				canvas.drawOval(oval, mCirclePaint);

				// canvas.drawCircle((float) (mCellSpace * (i + 0.5)), (float)
				// ((j + 0.5) * mCellSpace), mCellSpace / 3, mCirclePaint);
				break;

			case CLICK_TODAY:
				Log.d("Tag", "调用CLICK_TODAY");
				todayCanClick = false;
				mTextPaint.setColor(Color.parseColor(whiteColor));
				mTextPaint.setStrokeWidth(3);
				mCirclePaint.setColor(todayGradientColor);

				RectF oval1 = new RectF(todayLeft, startTop, todayRight, startBottom);
				canvas.drawOval(oval1, mCirclePaint);

				// canvas.drawCircle((float) (mCellSpace * (i + 0.5)), (float)
				// ((j + 0.5) * mCellSpace), mCellSpace / 3, mCirclePaint);
				canvas.drawLine((float) ((i + 0.5) * mCellSpace - measureValue / 2), (float) ((j + 0.7) * mCellSpace), (float) ((i + 0.5) * mCellSpace + measureValue / 2),
						(float) ((j + 0.7) * mCellSpace), mTextPaint);
				break;
			}

			canvas.drawText(content, (float) ((i + 0.5) * mCellSpace - measureValue / 2), (float) ((j + 0.7) * mCellSpace - mTextPaint.measureText(content, 0, 1) / 2), mTextPaint);
		}
	}

	/**
	 * 保存的两个日期13、14，默认是13是昨天，14是今天，但有可能用户改了日期，设置13号为今天，所以就有假的今天和昨天
	 * 
	 */
	public enum State {
		CURRENT_MONTH_DAY, PAST_MONTH_DAY, NEXT_MONTH_DAY, PUNCH_DAY, TODAY, YESTERDAY, TODAY_AND_PUNCH, /* 今天打过卡 */
		TODAY_AND_NO_PUNCH, /* 今天未打卡 */
		YESTERDAY_AND_NO_PUNCH, /* 昨天未打卡 */
		CLICK_YESTERDAY, /* 点击昨天 */
		CLICK_TODAY, /* 点击今天 */
		FAKE_YESTERDAY_AND_NO_PUNCH, /* 是两个日期中大的，但不是手机端的‘今天’ */
		FAKE_TODAY_AND_NO_PUNCH; /* 是两个日期中小的，而且是手机端的‘今天’ */
	}
}
